package com.ymh.git.thread.juc.threadpool;

import com.ymh.git.thread.juc.SleepTools;

import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by aivin on 2018/11/7.
 * <p>
 * 执行我们的定时任务
 */
public class ScheduledCase {


    public static void main(String[] args) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);

        //只执行一次
        scheduledThreadPoolExecutor.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println(" the task run once");
            }
        }, 3000, TimeUnit.MICROSECONDS);


        //固定延时间隔
        scheduledThreadPoolExecutor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("FixedDelay start" + ScheduleWorker.simpleDateFormat.format(new Date()));
                SleepTools.second(2);
                System.out.println("FixedDelay end" + ScheduleWorker.simpleDateFormat.format(new Date()));
            }
        }, 1000, 3000, TimeUnit.MICROSECONDS);


        scheduledThreadPoolExecutor.scheduleAtFixedRate(new ScheduleWorker(ScheduleWorker.Normal), 1000, 3000, TimeUnit.MILLISECONDS);
        scheduledThreadPoolExecutor.scheduleAtFixedRate(new ScheduleWorker(ScheduleWorker.HasException), 1000, 3000, TimeUnit.MILLISECONDS);
        scheduledThreadPoolExecutor.scheduleAtFixedRate(new ScheduleWorker(ScheduleWorker.ProcessException), 1000, 3000, TimeUnit.MILLISECONDS);
    }
}
